<?php

/**
 * 话题关联的wiki
 * 每天4点10分跑一次数据，更新话题关联点wiki数据
 *
 **/


ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\wiki\GoodsHot;
use Biz\cache\wiki\TopicWiki as cTopicWiki;
use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\service\wiki\ObjRelateWiki;
use Doraemon\cache\TopicCounter;
use Doraemon\service\Elasticsearch\Photo as esPhotoService;
use Biz\module\Classes\wiki\TopicWiki;
use Doraemon\tools\Log;
use Doraemon\model\Topic\TopicActivity as TopicActivityModel;
use Doraemon\service\Topic\TopicActivity;

$topicActivityId = $argv[1] ?? 0;

//除了家你还在哪开过party
$start = time();
$page = 1;
$pageSize = 1000;
$list = [];
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $where = [
        'LIMIT' => $limit
    ];
    if ($topicActivityId) {
        $where['id'] = $topicActivityId;
    }
    $list = TopicActivityModel::select('*', $where);
    foreach ($list as $row) {
        if ($row['status'] != TopicActivityModel::STATUS_SUCCESS) {//话题状态：未上线
            TopicCounter::setWikisNumByTopicId($row['id'], 0);
            cTopicWiki::delTopic($row['id']);
        } else {
            $topicId = $row['id'];
            $topicTitle = $row['title'];
            $page = 1;
            $pageSize = 500;
            $scrollId = '';
            $allNoteIds = [];
            while (true) {
                $res = (new esPhotoService)->scrollPhotosIdByTopic($topicTitle, $page, $pageSize, $scrollId);
                $scrollId = !empty($res['scroll_id']) ? $res['scroll_id'] : $scrollId;
                $ids = $res ? array_column($res['list'], 'id') : [];
                if (empty($ids)) {
                    $scrollId ? (new esPhotoService)->clearScroll($scrollId) : '';
                    break;
                }
                $allNoteIds = array_merge($allNoteIds, $ids);
                $page++;
            }
            $count = count($allNoteIds);
            var_dump('话题#' . $topicId . '#' . $topicTitle . '#查询出note数量:' . $count);
            $noteIds = [];
            $page = 1;
            $pageSize = 1000;
            $wikiCount = 0;
            cTopicWiki::delTopic($topicId);
            do {
                $count = 0;
                $noteIds = array_slice($allNoteIds, ($page - 1) * $pageSize, $pageSize);
                if ($noteIds) {
                    $count = getTopicWikiIds($topicId, $topicTitle, $noteIds);
                }
                $wikiCount = $wikiCount + $count;
                $page++;
            } while ($noteIds);
            var_dump('话题#' . $topicId . '#' . $topicTitle . '#查询出wiki总数量:' . $wikiCount);
            ObjRelateWiki::changeTopicWikiNum($topicId);
        }
    }
    $page++;
} while ($list);
$end = time();
var_dump(date('Y-m-d-H-i-s') . '更新话题关联的wiki完成,总耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');

/**
 * 获取wiki_id集合
 */
function getTopicWikiIds($topicId, $topicTitle, $noteIds)
{
    if ($noteIds) {
        //获取 wiki_id
        $noteRelateWikiIds = mObjRelateWiki::select('wiki_id', [
            'obj_id' => $noteIds,
            'obj_type' => mObjRelateWiki::OBJ_TYPE_NOTE,
            'wiki_id[!]' => 0,
            'status' => mObjRelateWiki::STATUS_NORMAL
        ]);
        $noteRelateWikiIds = $noteRelateWikiIds ? $noteRelateWikiIds : [];

        //获取wiki列表
        $wikiList = mWiki::selectByWhere(['id' => $noteRelateWikiIds, 'status[!]' => mWiki::STATUS_DEL, 'is_default' => 1],
            ['id', 'basic_id', 'title']);
        $count = count($wikiList);
        var_dump('话题#' . $topicId . '#' . $topicTitle . '#查询到wiki数量:' . $count);

        //获取分词数组遍历
        $cateNames = TopicWiki::getCateNames($topicId, $topicTitle);

        if ($wikiList) {
            foreach ($wikiList as $row) {
                $basicId = $row['basic_id'];
                $wikiId = $row['id'];
                //获取商品品类热度
                $hot = GoodsHot::getGoodsHotById($basicId);
                $hot = $hot ? $hot : 0;
                $start_hot = $hot;
                $str = '';
                if ($cateNames) {
                    //遍历wiki是否存在这个标题
                    foreach ($cateNames as $cateName) {
                        if (strpos($row['title'], $cateName) !== false) {
                            //获取商品品类热度
                            $hot = $hot + 10000;
                            Log::info("topic_wiki加分记录", json_encode(["topic_id" => $topicId, "wiki_id" => $wikiId, "cate_name" => $cateNames, "title" => $row['title']]));
                            $str = "-加分了-topic_id:" . $topicId . "--wiki_id:" . $wikiId . "标题:" . $row['title'] . "分词分类信息:" . json_encode($cateNames, JSON_UNESCAPED_UNICODE);
                            break;
                        }
                    }
                }
                var_dump("开始热度:" . $start_hot . "---结束热度:" . $hot . $str);
                //遍历wiki_list 取 wiki_ids 中的wiki增加商品热度
                var_dump("增加redis话题id:" . $topicId . "---热度:" . $hot . "wiki_id:" . $wikiId);
                cTopicWiki::addWiki($topicId, $wikiId, $hot);
            }
        }
    }
    return $count;
}









